﻿//在RC中已经去掉了IValueProvider接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using BlogEngine.Core;

namespace _4mvcBlog.Core
{
    public class PostValueProvider //: IValueProvider
    {
        private static ControllerContext context;
        //private DefaultValueProvider dProvider;

        public PostValueProvider(ControllerContext context)
        {
            //this.context = context;
            //dProvider = new DefaultValueProvider(context);
        }

        //#region IValueProvider 成员

        //public ValueProviderResult GetValue(string name)
        //{
        //    if (string.IsNullOrEmpty(name))
        //    {
        //        throw new ArgumentException("参数不能为空", "name");
        //    }
        //    switch (name)
        //    {
        //        case "Tags":
        //            return GetTagsValue();
        //        case "Categories":
        //            return GetCategoriesValue();
        //        default:
        //            return new DefaultValueProvider(context).GetValue(name);
        //    }
        //}

        //#endregion

        public static IDictionary<string, ValueProviderResult> GetValueProvider(ControllerContext cContext)
        {
            context = cContext;
            //注意这里用的是Controller.ValueProvider，以使用系统的默认的form提交过来的值
            IDictionary<string, ValueProviderResult> dic = context.Controller.ValueProvider;
            dic["Tags"] = GetTagsValue();
            dic["Categories"] = GetCategoriesValue();
            return dic;
        }

        private static ValueProviderResult GetTagsValue()
        {
            string strTags = GetValueFromRequest("Tags");
            if (string.IsNullOrEmpty(strTags))
            {
                return null;
            }

            string[] tags = strTags.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            StateList<string> tagsList = new StateList<string>();
            foreach (string tag in tags)
            {
                tagsList.Add(tag.Trim().ToLowerInvariant());
            }

            return new ValueProviderResult(tagsList, strTags, CultureInfo.InvariantCulture);
        }

        private static ValueProviderResult GetCategoriesValue()
        {
            string strCategories = GetValueFromRequest("Categories");
            if (string.IsNullOrEmpty(strCategories))
            {
                return null;
            }

            string[] categories = strCategories.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            StateList<Category> list = new StateList<Category>();
            foreach (string c in categories)
            {
                list.Add(Category.GetCategory(new Guid(c)));
            }

            return new ValueProviderResult(list, strCategories, CultureInfo.InvariantCulture);
        }

        private static string GetValueFromRequest(string name)
        {
            string value = null;
            HttpRequestBase request = context.HttpContext.Request;
            if (request != null)
            {
                if (request.QueryString != null)
                {
                    value = request.QueryString[name];
                }
                if (string.IsNullOrEmpty(value) && (request.Form != null))
                {
                    value = request.Form[name];
                }
            }

            return value;
        }
    }
}
